package org.mech.sefer.entity.converter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang.Validate;

public abstract class AbstractConverter<FROM, TO> implements  Converter<FROM, TO> {
	
	protected abstract TO createInstance();
	
	protected abstract TO doConvert(FROM from, TO to);
	
	@Override
	public TO convert(final FROM from, TO to) {
		Validate.notNull(from);

		TO toObject = to;

		if (toObject == null) {
			toObject = createInstance();
		}

		Validate.notNull(toObject);

		return doConvert(from, toObject);
	};
	
	@Override
	public Collection<TO> convertAll(Collection<FROM> objects)
			throws ConversionException {
		List<TO> list = new ArrayList<TO>();
		
		for(FROM f : objects){
			list.add(convert(f, createInstance()));
		}

		return list;
	}

}
